﻿#include "ui_plandlg.h"
#include "plandlg.h"
#include <QTextStream>
#include <QFile>
#include <QMessageBox>
#include <QGraphicsDropShadowEffect>
#include <QListView>
#include <QDir>
#include <QSettings>
#include <QTextCodec>

PlanDlg::PlanDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PlanDlg)
{
    ui->setupUi(this);
    this->adjustSize();
    this->setAttribute(Qt::WA_DeleteOnClose);

    this->setWindowModality(Qt::ApplicationModal);
    this->setModal(true);
    this->setAttribute(Qt::WA_ShowModal,true);

    Qt::WindowFlags flags=Qt::Dialog;
    flags |=Qt::WindowMinMaxButtonsHint;
    flags |=Qt::WindowCloseButtonHint;
    this->setWindowFlags(flags);

    m_sql.init("PlanDlg_data");


    m_strNotItem = tr("Not Set");

    m_pMsg = new Message(this);

    QFile dlg_qss(":/qss/dlg.qss");
    if (dlg_qss.open(QFile::ReadOnly))
    {
        QTextStream filetext(&dlg_qss);
        QString stylesheet = filetext.readAll();
        this->setStyleSheet(stylesheet);
        dlg_qss.close();
    }
    QGraphicsDropShadowEffect *shadow_effectA = new QGraphicsDropShadowEffect(this);
    QGraphicsDropShadowEffect *shadow_effectB = new QGraphicsDropShadowEffect(this);
    QGraphicsDropShadowEffect *shadow_effectC = new QGraphicsDropShadowEffect(this);
    shadow_effectA->setOffset(0, 0);
    shadow_effectA->setColor(QColor(150,150,150));
    shadow_effectA->setBlurRadius(6);

    shadow_effectB->setOffset(0, 0);
    shadow_effectB->setColor(QColor(150,150,150));
    shadow_effectB->setBlurRadius(6);

    shadow_effectC->setOffset(0, 0);
    shadow_effectC->setColor(QColor(150,150,150));
    shadow_effectC->setBlurRadius(6);


    ui->Plan_gBox_1->setGraphicsEffect(shadow_effectA);
    ui->widget->setGraphicsEffect(shadow_effectB);
    ui->Plan_gBox_3->setGraphicsEffect(shadow_effectC);

    ui->comBox_defect->setView(new  QListView());
    ui->comBox_scan->setView(new  QListView());
    ui->comBox_wood->setView(new  QListView());

    ui->widget->iniPlanPage(0);
    connect(ui->Bt_Quite, &QPushButton::clicked,
            this, &PlanDlg::on_Bt_Quite_clicked);
    connect(ui->Bt_Save, &QPushButton::clicked,
            this, &PlanDlg::on_Bt_Save_clicked);
    connect(ui->Bt_Updata, &QPushButton::clicked,
            this, &PlanDlg::on_Bt_Updata_clicked);
}

PlanDlg::~PlanDlg()
{
    delete ui;
}

void PlanDlg::iniPage(int nIndex)
{
    m_nState = nIndex;
    ui->Bt_Add->show();
    ui->Bt_Delete->show();

    InitComBox();
    ui->Bt_Print->hide();
    switch (nIndex) {
    case 1: //add
        ui->labState->setText(tr("state:add"));
        ui->label_4->hide();
        ui->label_5->hide();
        ui->label_chang->hide();
        ui->label_create->hide();
        ui->Bt_Add->hide();
        ui->Bt_Delete->hide();
        ui->Bt_Updata->hide();
        break;
    case 2: // search
        ui->labState->setText(tr("state:search"));
        ui->Bt_Save->hide();
        ui->Bt_Add->hide();
        ui->Bt_Delete->hide();
        ui->Bt_Updata->hide();
        ui->Edit_plan->setEnabled(false);
        ui->comBox_defect->setEnabled(false);
        ui->comBox_wood->setEnabled(false);
        ui->comBox_scan->setEnabled(false);
        break;
    case 3: // change
        ui->labState->setText(tr("state:change"));
        ui->Bt_Save->hide();
        ui->Bt_Add->hide();
        ui->Bt_Delete->hide();
        break;
    }

}


void PlanDlg::showTabel(int id)
{
    m_nPlanID = id;
    T_PlanLine data = m_sql.queryPlanLine(id);
    ui->comBox_defect->setCurrentText(data.schemeName);
    ui->comBox_wood->setCurrentText(data.woodType);
    ui->comBox_scan->setCurrentText(data.scanName);
    ui->Edit_plan->setText(data.planName);
    ui->label_create->setText(data.createTime);
    ui->label_chang->setText(data.changeTime);
}

void PlanDlg::InitComBox()
{
    isInitializationComplete = false;
    QList<DefectScheme> recorders = m_sql.queryAllDefectSchemes();
    ui->comBox_defect->clear();
    ui->comBox_wood->clear();
    ui->comBox_scan->clear();

    ui->comBox_defect->addItem(m_strNotItem, QVariant(0));
    ui->comBox_wood->addItem(m_strNotItem);
    ui->comBox_scan->addItem(m_strNotItem);

    for (const DefectScheme& scheme : recorders) {
        ui->comBox_defect->addItem(scheme.schemeName, QVariant(scheme.schemeId));
    }

    QString Path = "configs/configs.ini";
    QSettings iniRead(Path, QSettings::IniFormat);
    iniRead.setIniCodec(QTextCodec::codecForName("UTF_8"));

    QString info;
    int nNum = iniRead.value("Type/num").toInt();
    for (int i=0;i<nNum;i++) {
        info = QString("WoodType/part_%1").arg(i+1);
        ui->comBox_wood->addItem(iniRead.value(info).toString());
    }

    ui->comBox_scan->addItem(tr("one-sided"));
    ui->comBox_scan->addItem(tr("double-sided"));
    ui->comBox_scan->addItem(tr("four-sided"));
    isInitializationComplete = true;
}



void PlanDlg::on_Bt_Save_clicked()
{
    QString strPlan;
    strPlan = ui->Edit_plan->text();
    if(strPlan.isEmpty()){
        m_pMsg->Push(MESSAGE_TYPE_WARNING, tr("The plan name cannot be empty!"));
        return;
    }
    T_PlanLine data;
    data.planName = strPlan;
    data.schemeName = ui->comBox_defect->currentText();
    data.scanName = ui->comBox_scan->currentText();
    data.woodType = ui->comBox_wood->currentText();
    data.schemeId = ui->comBox_defect->currentData().toInt();

    if(m_sql.insertPlanLine(data)){
        m_pMsg->Push(MESSAGE_TYPE_SUCCESS, tr("The plan data insert success!"));
        this->close();
        emit sigFormInfo();
    }else{
        m_pMsg->Push(MESSAGE_TYPE_WARNING, tr("The plan data insert warning!"));
        return;
    }
}

void PlanDlg::on_Bt_Print_clicked()
{
    //    LimeReport::ReportEngine* m_report;
    //    m_report = new LimeReport::ReportEngine();
    //    InitReport(m_report);
    //    QString ReportPath = QDir::currentPath() + "/Report/planline.lrxml";
    //    m_report->loadFromFile(ReportPath);
    ////    m_report->previewReport();
    //    m_report->designReport();
}

void PlanDlg::on_comBox_defect_currentIndexChanged(int index)
{
    if(!isInitializationComplete)return;
    if (index >= 0) {

        QVariant data = ui->comBox_defect->itemData(index);
        int schemeId = data.toInt();
        ui->widget->iniPage(2);
        ui->widget->upPageData(schemeId);
    }
}


void PlanDlg::on_Bt_Quite_clicked()
{
    this->close();
}


void PlanDlg::on_Bt_Updata_clicked()
{
    QString strPlan;
    strPlan = ui->Edit_plan->text();
    if(strPlan.isEmpty()){
        m_pMsg->Push(MESSAGE_TYPE_WARNING, tr("The plan name cannot be empty!"));
        return;
    }
    T_PlanLine data;
    data.id = m_nPlanID;
    data.planName = strPlan;
    data.schemeName = ui->comBox_defect->currentText();
    data.scanName = ui->comBox_scan->currentText();
    data.woodType = ui->comBox_wood->currentText();
    data.schemeId = ui->comBox_defect->currentData().toInt();

    if(m_sql.updatePlanLine(data)){
        m_pMsg->Push(MESSAGE_TYPE_SUCCESS, tr("The plan data insert success!"));
        this->close();
        emit sigFormInfo();
    }else{
        m_pMsg->Push(MESSAGE_TYPE_WARNING, tr("The plan data insert warning!"));
        return;
    }
}

